cmake_minimum_required(VERSION 3.22.1)
#####################################################
# 项目名称
set(TARGET_NAME "app" CACHE STRING "项目名称")
# 编译目录
set(BUILD_DIR ${CMAKE_CURRENT_LIST_DIR}/build CACHE STRING "项目编译目录")
# 输出目录
set(OUT_DIR ${CMAKE_CURRENT_LIST_DIR}/out CACHE STRING "项目输出目录")
# 设置Kconfig脚本路径
set(KCONFIG_PY ${CMAKE_CURRENT_LIST_DIR}/script/Kconfig.py)
#######################设置项目名称############################################
# 设置项目名称
project(${TARGET_NAME})
# 设置语言
enable_language(C CXX ASM)

#######################检查系统环境############################################
# 查找交叉编译器
find_program(python_path python3)
if(NOT IS_ABSOLUTE ${python_path})
    message(FATAL_ERROR "please install python3!")
endif()

#################导入Kconfig的目标##########################
########################################################
# 2. Kconfig-aware extensions
########################################################
#
# Kconfig is a configuration language developed for the Linux
# kernel. The below functions integrate CMake with Kconfig.
#
# 2.1 Misc
#
# import_kconfig(<prefix> <kconfig_fragment> [<keys>])
#
# Parse a KConfig fragment (typically with extension .config) and
# introduce all the symbols that are prefixed with 'prefix' into the
# CMake namespace. List all created variable names in the 'keys'
# output variable if present.
function(import_kconfig prefix kconfig_fragment)
  # Parse the lines prefixed with 'prefix' in ${kconfig_fragment}
  file(
    STRINGS
    ${kconfig_fragment}
    DOT_CONFIG_LIST
    REGEX "^${prefix}"
    ENCODING "UTF-8"
  )

  foreach (CONFIG ${DOT_CONFIG_LIST})
    # CONFIG could look like: CONFIG_NET_BUF=y

    # Match the first part, the variable name
    string(REGEX MATCH "[^=]+" CONF_VARIABLE_NAME ${CONFIG})

    # Match the second part, variable value
    string(REGEX MATCH "=(.+$)" CONF_VARIABLE_VALUE ${CONFIG})
    # The variable name match we just did included the '=' symbol. To just get the
    # part on the RHS we use match group 1
    set(CONF_VARIABLE_VALUE ${CMAKE_MATCH_1})

    if("${CONF_VARIABLE_VALUE}" MATCHES "^\"(.*)\"$") # Is surrounded by quotes
      set(CONF_VARIABLE_VALUE ${CMAKE_MATCH_1})
    endif()

    set("${CONF_VARIABLE_NAME}" "${CONF_VARIABLE_VALUE}" PARENT_SCOPE)
    list(APPEND keys "${CONF_VARIABLE_NAME}")
  endforeach()

  foreach(outvar ${ARGN})
    set(${outvar} "${keys}" PARENT_SCOPE)
  endforeach()
endfunction()

# 必须在配置阶段就执行
execute_process(
  COMMAND
    ${python_path} ${KCONFIG_PY} ${CMAKE_CURRENT_LIST_DIR}/Kconfig ${BUILD_DIR}/.config  ${BUILD_DIR}/autoconfig.h ${BUILD_DIR}/log.txt ${CMAKE_CURRENT_LIST_DIR}/prj.conf
  RESULT_VARIABLE kconfig_lib_result
  OUTPUT_VARIABLE kconfig_lib_stdout
  ERROR_VARIABLE kconfig_lib_stderr
  OUTPUT_STRIP_TRAILING_WHITESPACE
  ERROR_STRIP_TRAILING_WHITESPACE
)
if (kconfig_lib_result)
message(FATAL_ERROR "${KCONFIG_PY} is runing error,please kconfiglib!")
else()
message(STATUS "${kconfig_lib_stdout}")
endif()
import_kconfig(CONFIG_ ${BUILD_DIR}/.config)

#############################设置编译选项##################################
set(CMAKE_C_FLAGS "-include ${BUILD_DIR}/autoconfig.h")

#########################设置可执行目标#####################
add_executable(${TARGET_NAME} "")

#####################设置编译文件###########################
if(CONFIG_MODULE_1_ENABLE)
target_sources(${TARGET_NAME} PUBLIC
    ${CMAKE_CURRENT_LIST_DIR}/source/module_1/module_1.c
)
target_include_directories(${TARGET_NAME} PUBLIC
    ${CMAKE_CURRENT_LIST_DIR}/source/module_1/
)
endif()

if(CONFIG_MODULE_2_ENABLE)
target_sources(${TARGET_NAME} PUBLIC
    ${CMAKE_CURRENT_LIST_DIR}/source/module_2/module_2.c
)
target_include_directories(${TARGET_NAME} PUBLIC
    ${CMAKE_CURRENT_LIST_DIR}/source/module_2/
)
endif()

target_sources(${TARGET_NAME} PUBLIC
    ${CMAKE_CURRENT_LIST_DIR}/source/main.c
)
############################设置安装目录###################################
install(PROGRAMS 
    ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}
    DESTINATION ${OUT_DIR}
)
